/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice.dropdown;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.BaseDialog;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;

import com.xuexiang.xui_lib.util.ViewModel;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.view.Animationtextview;
import com.xuexiang.xuidemo.view.Dropdowntextview;
import com.xuexiang.xuidemo.view.TitleBarComponent;

/**
 * 第一个导航页面
 *
 * @since 2021-03-30
 */
public class DropdownOneSlice extends AbilitySlice implements Component.ClickedListener,
    BaseDialog.DialogListener, Dropdowntextview.ListContainerview {
    private Context context;
    private TitleBarComponent dropdownonetitle;
    private Dropdowntextview viewdrop;
    private Dropdowntextview viewdrop1;
    private Dropdowntextview viewdrop2;
    private Dropdowntextview viewdrop3;
    private Dropdowntextview viewdrop4;
    private Dropdowntextview viewdrop4One;
    private Dropdowntextview viewdrop5;
    private Dropdowntextview viewdrop5One;
    private Dropdowntextview viewdrop6;
    private Dropdowntextview viewdrop6One;
    private String[] data = null;
    private String[] data1 = null;
    private Button buttontk;
    private Button bjqhbutton;
    private Component componentview;
    private DependentLayout wholeview;
    private PopupDialog popupDialog;
    private Animationtextview dropatview;
    private DirectionalLayout dtlayoutview;
    private Button button;
    private Button button1;
    private final int anInt1 = 1;
    private final int anInt2 = 2;
    private final int anInt5 = 5;
    private final int anInt100 = 100;
    private final int anInt110 = 110;
    private final int anInt130 = 130;
    private final int anInt140 = 140;
    private final int anInt200 = 200;
    private final int anInt300 = 300;
    private final int anInt500 = 500;
    private final int anInt900 = 900;

    /**
     * 加载view
     *
     * @param intent
     */
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_dropdown_onelayout);
        context = this;
        data = new ViewModel(this).getStringResource(ResourceTable.Strarray_drop_array);
        data1 = new ViewModel(this).getStringResource(ResourceTable.Strarray_drop_array2);
        initview();
        initdata();
    }

    private void initview() {
        dropatview = (Animationtextview) findComponentById(ResourceTable.Id_drop_atview);

        componentview = findComponentById(ResourceTable.Id_componentpupub);
        wholeview = (DependentLayout) findComponentById(ResourceTable.Id_wholeview);
        dropdownonetitle = (TitleBarComponent) findComponentById(ResourceTable.Id_dropdown_onetitle);
        dropdownonetitle.setAbility(this);
        dropdownonetitle.setTitile("统一的下拉框样式");
        viewdrop = (Dropdowntextview) findComponentById(ResourceTable.Id_viewdrop);
        viewdrop.setStringArray(data);
        viewdrop.settexttypewith(anInt130);

        viewdrop1 = (Dropdowntextview) findComponentById(ResourceTable.Id_viewdrop1);
        viewdrop1.setStringArray(data);

        viewdrop2 = (Dropdowntextview) findComponentById(ResourceTable.Id_viewdrop2);
        viewdrop2.setStringArray(data);
        viewdrop2.settexttypewith(anInt110);

        viewdrop3 = (Dropdowntextview) findComponentById(ResourceTable.Id_viewdrop3);
        viewdrop3.setStringArray(data);

        viewdrop4 = (Dropdowntextview) findComponentById(ResourceTable.Id_viewdrop4);
        viewdrop4.setStringArray(data);
        viewdrop4.setTextContent(data[anInt2]);
        viewdrop4.setListview(this::onItemClickedtext);

        viewdrop4One = (Dropdowntextview) findComponentById(ResourceTable.Id_viewdrop4_1);
        viewdrop4One.setStringArray(data);
        viewdrop4One.setnocilik(false);

        viewdrop5 = (Dropdowntextview) findComponentById(ResourceTable.Id_viewdrop5);
        viewdrop5.setTextContent(data[anInt5]);
        viewdrop5.setStringArray(data1);
        viewdrop5.settexttypewith(anInt100);

        viewdrop5One = (Dropdowntextview) findComponentById(ResourceTable.Id_viewdrop5_1);
        viewdrop5One.setStringArray(data);
        viewdrop5One.setlistCnerhigde(anInt300); // listCnerhigde = anInt300
        viewdrop6 = (Dropdowntextview) findComponentById(ResourceTable.Id_viewdrop6);
        viewdrop6.setStringArray(data);
        viewdrop6One = (Dropdowntextview) findComponentById(ResourceTable.Id_viewdrop6_1);
        viewdrop6One.setStringArray(data);

        buttontk = (Button) findComponentById(ResourceTable.Id_buttontk);
        buttontk.setClickedListener(this::onClick);
        bjqhbutton = (Button) findComponentById(ResourceTable.Id_bjqhbutton);
        bjqhbutton.setClickedListener(this::onClick);
    }

    private void initdata() {
        Element touchedBackGround = ElementScatter.getInstance(this)
            .parse(ResourceTable.Graphic_backfroud_search);
        viewdrop.setbackgroud(touchedBackGround);
        viewdrop3.setTextContent(data[anInt1]);
        viewdrop5One.settexttypewith(anInt110);
        viewdrop6.settextfile();
        viewdrop6.settexttypewith(anInt140);
        viewdrop6.setnocilik(false); // 是否可以编辑
        viewdrop6One.settextfile();
        viewdrop6One.settexttypewith(anInt140);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_buttontk:
                componentview.setVisibility(Component.VISIBLE);
                compenDialog();

                break;
            case ResourceTable.Id_bjqhbutton:
                if (viewdrop4.getisClick()) {
                    viewdrop4.setnocilik(false);
                    viewdrop6.setnocilik(false);
                } else {
                    viewdrop4.setnocilik(true);
                    viewdrop6.setnocilik(true);
                }
                break;
            default:
                break;
        }
    }

    private void compenDialog() {
        popupDialog = new PopupDialog(context, wholeview);
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dropdown_dialogview,
            null, false);
        dtlayoutview = (DirectionalLayout) component.findComponentById(ResourceTable.Id_dtlayoutview);
        popupDialog.setCustomComponent(component);
        popupDialog.setSize(anInt900, anInt500);
        popupDialog.setAlignment(LayoutAlignment.CENTER);
        popupDialog.setAutoClosable(true);
        popupDialog.setDialogListener(this::isTouchOutside);
        Element touched = ElementScatter.getInstance(this)
            .parse(ResourceTable.Graphic_selector_page2);
        Dropdowntextview dropdownText = (Dropdowntextview) component.findComponentById(ResourceTable.Id_viewdrop);
        dropdownText.setStringArray(data);
        dropdownText.setbackgroud(touched);
        button = (Button) component.findComponentById(ResourceTable.Id_drop_dialog_cancle);
        button1 = (Button) component.findComponentById(ResourceTable.Id_drop_dialog_ok);
        dropdownText.settextfile();
        dropdownText.settexttypewith(anInt200);

        viewdrop.setPosition(dtlayoutview);
        popupDialog.show();
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                popupDialog.hide();
                componentview.setVisibility(Component.HIDE);
            }
        });
        button1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                popupDialog.hide();
                componentview.setVisibility(Component.HIDE);
            }
        });
    }

    @Override
    public boolean isTouchOutside() {
        popupDialog.hide();
        componentview.setVisibility(Component.HIDE);
        return false;
    }

    @Override
    public void onItemClickedtext(String data2) {
        dropatview.seteffect(dropatview, data2, wholeview);
    }
}
